package com.huatai.bi.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class LocalDateTimeUtils {

    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";


    /*===========================日期============================*/
    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentDateTime(){
        return getCurrentDate(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentDate(){
        return getCurrentDate(YYYY_MM_DD);
    }

    /**
     * 根据时间获取指定格式字符串
     * @return
     */
    public static String getCurrentDate(String pattern){
        LocalDateTime now = LocalDateTime.now();
        return getDate(now,pattern);
    }

    /**
     * 根据时间获取指定格式字符串
     * @return
     */
    public static String getDate(LocalDateTime now,String pattern){
        return now.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 指定日期加N天
     * @return
     */
    public static String plusDays(int days){
        return plusDays(days,YYYY_MM_DD);
    }

    /**
     * 指定日期加N天
     * @return
     */
    public static String plusDays(int days,String pattern){
        return plusDays(LocalDateTime.now(),days,pattern);
    }

    /**
     * 指定日期加N天
     * @return
     */
    public static String plusDays(LocalDateTime now,int days,String pattern){
        LocalDateTime targetTime = now.plusDays(days);
        return targetTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 指定日期减N天
     * @return
     */
    public static String minusDays(int days){
        return minusDays(days,YYYY_MM_DD);
    }

    /**
     * 指定日期减N天
     * @return
     */
    public static String minusDays(int days,String pattern){
        return minusDays(LocalDateTime.now(),days,pattern);
    }

    /**
     * 指定日期减N天
     * @return
     */
    public static String minusDays(LocalDateTime now,int days,String pattern){
        LocalDateTime targetTime = now.minusDays(days);
        return targetTime.format(DateTimeFormatter.ofPattern(pattern));
    }


    /*===========================周============================*/
    /**
     * 获取本周第一天
     * @return
     */
    public static String weekFirstDate(){
        return weekDate(YYYY_MM_DD,TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    /**
     * 获取本周最后一天
     * @return
     */
    public static String weekEndDate(){
        return weekDate(YYYY_MM_DD,TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
    }

    /**
     * 获取本周周几日期
     * @return
     */
    public static String weekDate(String pattern,TemporalAdjuster adjuster){
        LocalDateTime now = LocalDateTime.now();
        return weekDate(now,pattern,adjuster);
    }

    /**
     * 获取指定日期上一个或下一个周几日期
     * @return
     */
    public static String weekDate(LocalDateTime now,String pattern,TemporalAdjuster adjuster){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return now.with( adjuster).format(formatter);
    }


    /*===========================月============================*/

    /**
     * 获取本月第一天
     * @return
     */
    public static String monthFirstDate(){
        return monthDate(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取本月最后一天
     * @return
     */
    public static String monthEndDate(){
        return monthDate(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取本月第几天日期
     * @return
     */
    public static String monthDate(TemporalAdjuster adjuster){
        return monthDate(LocalDateTime.now(),YYYY_MM_DD,adjuster);
    }

    /**
     * 获取指定日期月内第几天日期
     * @return
     */
    public static String monthDate(LocalDateTime now,String pattern,TemporalAdjuster adjuster){
        return getDate(now.with(adjuster),pattern);
    }



    /*===========================季度============================*/

    /**
     * 获取本季度第一天
     * @return
     */
    public static String quarterFirstDate(){
        return quarterFirstDate(LocalDateTime.now(),YYYY_MM_DD);
    }

    /**
     * 获取指定日期的季度第一天
     * @return
     */
    public static String quarterFirstDate(LocalDateTime now,String pattern){
        int currentMonth = now.getMonthValue();
        int startMonth = ((currentMonth-1)/3)*3+1;
        LocalDate startDate = LocalDate.of(now.getYear(), startMonth, 1);
        return startDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取本季度最后一天
     * @return
     */
    public static String quarterEndDate(){
        return quarterEndDate(LocalDateTime.now(),YYYY_MM_DD);
    }


    /**
     * 获取指定日期季度最后一天
     * @return
     */
    public static String quarterEndDate(LocalDateTime now,String pattern){
        int currentMonth = now.getMonthValue();
        int startMonth = ((currentMonth-1)/3)*3+1;
        LocalDate endDate = LocalDate.of(now.getYear(), startMonth+2, 1);
        endDate = endDate.plusDays(endDate.lengthOfMonth()-1);
        return endDate.format(DateTimeFormatter.ofPattern(pattern));
    }
}
